import ij

def createDatabase(title='', date='', location='', description='',
				   rusers='', wusers='', rgroups='', wgroups=''):
	if rusers!='': rusers=lst2str(rusers)
	if wusers!='': wusers=lst2str(wusers)
	if rgroups!='': rgroups=lst2str(rgroups)
	else: rgroups='users'
	if wgroups!='': wgroups=lst2str(wgroups)
	else: wgroups='admin'
	gd=ij.gui.GenericDialog("Create database to store image")
	gd.addStringField("Title:", title, 50)
	gd.addStringField("Date:", date, 50)
	gd.addStringField("Location:", location, 50)
	gd.addStringField("Description:", description, 50)
	gd.addMessage("Use ';' to separate multiple users or groups.")
	gd.addStringField("Read users:", rusers, 50)
	gd.addStringField("Write users:", wusers, 50)
	gd.addStringField("Read groups:", rgroups, 50)
	gd.addStringField("Write groups:", wgroups, 50)
	gd.showDialog()
	if gd.wasCanceled(): return
	title       = gd.getNextString()
	date        = gd.getNextString()
	location    = gd.getNextString()
	description = gd.getNextString()
	rusers      = gd.getNextString().split(";")
	wusers      = gd.getNextString().split(";")
	rgroups     = gd.getNextString().split(";")
	wgroups     = gd.getNextString().split(";")
	return [title, date, location, description, rusers, wusers, rgroups, wgroups]

def selectUser(usernames, realnames):
	choices=[]
	for n in range(len(usernames)):
		choices.append("%s: %s" % (usernames[n], realnames[n]))
	gd=ij.gui.GenericDialog("Choose a user...")
	gd.addChoice("Usernames/Realnames:", choices, choices[n])
	gd.showDialog()
	if gd.wasCanceled(): return
	choice=gd.getNextChoice()
	return usernames[choices.index(choice)]

def selectImageName(name='', description=''):
	gd=ij.gui.GenericDialog("Select image name and description")
	gd.addStringField("Name:", name, 50)
	gd.addStringField("Description:", description, 50)
	gd.showDialog()
	if gd.wasCanceled(): return
	title       = gd.getNextString()
	description = gd.getNextString()
	return [name, description]

def lst2str(lst):
	str=''
	for x in lst:
		if x!='': str=str+';'+x
	return str[1:]
